<html><head>
   <title>Encoded Source Files</title>
   <meta name="generator"  content="HelpMaker.net">
   <meta name="keywords"  content="ANSI,UTF-8,CodecEditor"></head>
<body bgcolor="#FFFCEA">
<table width="100%"  border="0"  cellspacing="0"  cellpadding="2"  bgcolor="#C0C0C0">
  <tr>
    <td align="left">
      <div align="left"><font face="Arial"  color="#010101"  size="4"><span style=" font-size:14pt; font-family:Arial; color:#010101">Encoded Python Source Files</span></font></div>

    </td>
    <td align="right">
      <font face="Arial"  size="2">
        <a href="codeanddebuggerhints.htm">Previous</a>&nbsp;<a href="pyscripter.htm">Top</a>&nbsp;<a href="editorfonts.htm">Next</a>
      </font>
    </td>
  </tr></table>
<hr><div align="left"><font face="Arial"  color="#010101"  size="2"><span style=" font-size:10pt; font-family:Arial; color:#010101"><br></span></font></div><div align="left"><font face="Arial"  color="#010101"  size="2"><span style=" font-size:10pt; font-family:Arial; color:#010101">PyScripter supports the </span></font><a href="http://www.python.org/dev/peps/pep-0263/"><font face="Arial"  color="#0000ff"  size="2"><span style=" font-size:10pt; font-family:Arial; color:#0000ff"><u>PEP 263</u></span></font></a><font face="Arial"  color="#010101"  size="2"><span style=" font-size:10pt; font-family:Arial; color:#010101"> fully.&nbsp; The editor internally uses Unicode strings.&nbsp; When 
saved, Python files can be encoded in either utf-8 or ansi encoding.</span></font></div><div align="left"><font face="Arial"  color="#010101"  size="2"><span style=" font-size:10pt; font-family:Arial; color:#010101"><br></span></font></div><div align="left"><font face="Arial"  color="#010101"  size="2"><span style=" font-size:10pt; font-family:Arial; color:#010101"><b>UTF-8 encoded source files</b></span></font></div><div align="left"><font face="Arial"  color="#010101"  size="2"><span style=" font-size:10pt; font-family:Arial; color:#010101"><br></span></font></div><div align="left"><font face="Arial"  color="#010101"  size="2"><span style=" font-size:10pt; font-family:Arial; color:#010101">You can select this encoding from the File Formats submenu of the </span></font><a href="editmenu.htm"><font face="Arial"  color="#0000ff"  size="2"><span style=" font-size:10pt; font-family:Arial; color:#0000ff"><u>Edit 
menu</u></span></font></a><font face="Arial"  color="#010101"  size="2"><span style=" font-size:10pt; font-family:Arial; color:#010101">.&nbsp; From that 
menu you can select whether UTF-8 encoded source files include the BOM UTF-8 signature 
which is detected by the Python interpreter.&nbsp; This signature is also detected by PyScripter 
when a file is loaded and other Windows editors. Although it is not necessary you are advised 
to include an encoding comment such as</span></font></div><div align="left"><font face="Arial"  color="#010101"  size="2"><span style=" font-size:10pt; font-family:Arial; color:#010101"><br></span></font></div><div align="center"><font face="Arial"  color="#010101"  size="2"><span style=" font-size:10pt; font-family:Arial; color:#010101"><i># -*- coding: utf-8 -*-</i></span></font></div><div align="left"><font face="Arial"  color="#010101"  size="2"><span style=" font-size:10pt; font-family:Arial; color:#010101"><br></span></font></div><div align="left"><font face="Arial"  color="#010101"  size="2"><span style=" font-size:10pt; font-family:Arial; color:#010101">as the first or second line of the python script.&nbsp; The advantage of using UTF-8 encoded files is 
that they can run without modification in other computers with different default encoding. <i>When using UTF-8 encoding you should specify all strings that are not plain ascii as python 
unicode stings by adding the prefix 'u'.</i></span></font></div><div align="left"><font face="Arial"  color="#010101"  size="2"><span style=" font-size:10pt; font-family:Arial; color:#010101"><i><br></i></span></font></div><div align="left"><font face="Arial"  color="#010101"  size="2"><span style=" font-size:10pt; font-family:Arial; color:#010101"><b>ANSI encoded files</b></span></font></div><div align="left"><font face="Arial"  color="#010101"  size="2"><span style=" font-size:10pt; font-family:Arial; color:#010101"><br></span></font></div><div align="left"><font face="Arial"  color="#010101"  size="2"><span style=" font-size:10pt; font-family:Arial; color:#010101">If the UTF-8 flag of the File Formats submenu of the </span></font><a href="editmenu.htm"><font face="Arial"  color="#0000ff"  size="2"><span style=" font-size:10pt; font-family:Arial; color:#0000ff"><u>Edit menu</u></span></font></a><font face="Arial"  color="#010101"  size="2"><span style=" font-size:10pt; font-family:Arial; color:#010101"> is not selected, then the file is 
treated as an ANSI string.&nbsp;&nbsp;&nbsp; To define a specific source code encoding, a magic comment 
must be placed into the source files either as first or second line in the file, e.g.:&nbsp;&nbsp;&nbsp; </span></font></div><div align="left"><font face="Arial"  color="#010101"  size="2"><span style=" font-size:10pt; font-family:Arial; color:#010101"><br></span></font></div><div align="left"><font face="Arial"  color="#010101"  size="2"><span style=" font-size:10pt; font-family:Arial; color:#010101">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; #!/usr/bin/python</span></font></div><div align="left"><font face="Arial"  color="#010101"  size="2"><span style=" font-size:10pt; font-family:Arial; color:#010101">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # -*- coding: &lt;encoding name&gt; -*-</span></font></div><div align="left"><font face="Arial"  color="#010101"  size="2"><span style=" font-size:10pt; font-family:Arial; color:#010101"><br></span></font></div><div align="left"><font face="Arial"  color="#010101"  size="2"><span style=" font-size:10pt; font-family:Arial; color:#010101">More precisely, the first or second line must match the regular expression &quot;coding[:=]\s*([-\w.]+)&quot;. The first group of this expression is then interpreted as encoding name. If the 
encoding is unknown to Python, an error is raised during compilation. There must not be any 
Python statement on the line that contains the encoding declaration.&nbsp; If such a comment is not 
present then the default system encoding is assumed.&nbsp; PyScripter detects such comments 
when it loads Python Source files and decodes them to Unicode using the appropriate 
encoding.</span></font></div><div align="left"><font face="Arial"  color="#010101"  size="2"><span style=" font-size:10pt; font-family:Arial; color:#010101"><br></span></font></div><div align="left"><font face="Arial"  color="#010101"  size="2"><span style=" font-size:10pt; font-family:Arial; color:#010101">The default python encoding is controlled by a Python file called &quot;site.py&quot;&nbsp; which is located in 
the python lib directory (see function &quot;setencoding&quot; in site.py). The default encoding when 
python is installed is ascii, which does not support non-ascii characters (character value 
greater than 127).&nbsp; If you are planning to use non-ascii strings in Python without using the utf-8 encoding, you will need to modify site.py and enable support for a locale aware default 
string encoding.</span></font></div><div align="left"><font face="Arial"  color="#010101"  size="2"><span style=" font-size:10pt; font-family:Arial; color:#010101"><br></span></font></div><div align="left"><font face="Arial"  color="#010101"  size="2"><span style=" font-size:10pt; font-family:Arial; color:#010101"><b>IDE encoding options for new files</b></span></font></div><div align="left"><font face="Arial"  color="#010101"  size="2"><span style=" font-size:10pt; font-family:Arial; color:#010101"><br></span></font></div><div align="left"><font face="Arial"  color="#010101"  size="2"><span style=" font-size:10pt; font-family:Arial; color:#010101">Pyscripter provides two </span></font><a href="ideoptions.htm"><font face="Arial"  color="#0000ff"  size="2"><span style=" font-size:10pt; font-family:Arial; color:#0000ff"><u>IDE options</u></span></font></a><font face="Arial"  color="#010101"  size="2"><span style=" font-size:10pt; font-family:Arial; color:#010101"> controlling the encoding of new files:</span></font></div><div align="left"><font face="Arial"  color="#010101"  size="2"><span style=" font-size:10pt; font-family:Arial; color:#010101"><br></span></font></div><div align="left"  style="margin-left:6mm; margin-right:0mm; text-indent:-6mm; margin-top:0.00mm; margin-bottom:0.00mm;"><font face="Symbol"  color="#010101"  size="2"><span style=" font-size:10pt; font-family:Symbol; color:#010101">&middot;</span></font><font face="Arial"  color="#010101"  size="2"><span style=" font-size:10pt; font-family:Arial; color:#010101">&nbsp;&nbsp; &nbsp;&nbsp;<i>Default line breaks for new files</i></span></font></div><div align="left"  style="margin-left:6mm; margin-right:0mm; text-indent:-6mm; margin-top:0.00mm; margin-bottom:0.00mm;"><font face="Symbol"  color="#010101"  size="2"><span style=" font-size:10pt; font-family:Symbol; color:#010101">&middot;</span></font><font face="Arial"  color="#010101"  size="2"><span style=" font-size:10pt; font-family:Arial; color:#010101">&nbsp;&nbsp; &nbsp;&nbsp;<i>Default encoding for new files</i></span></font></div><div align="left"><font face="Arial"  color="#010101"  size="2"><span style=" font-size:10pt; font-family:Arial; color:#010101"><br></span></font></div><div align="left"><font face="Arial"  color="#010101"  size="2"><span style=" font-size:10pt; font-family:Arial; color:#010101"><b>IDE option for detecting UTF-8 encoding when opening files</b></span></font></div><div align="left"><font face="Arial"  color="#010101"  size="2"><span style=" font-size:10pt; font-family:Arial; color:#010101"><br></span></font></div><div align="left"><font face="Arial"  color="#010101"  size="2"><span style=" font-size:10pt; font-family:Arial; color:#010101">Another IDE option (<i>Detect UTF-8 when opening files</i>) controls whether PyScripter attempts 
to detect utf-8 encoding when opening files without the BOM mark.&nbsp; This detection is done by 
analyzing the first 4000 characters of the file and is imperfect.&nbsp; It only applies to non-Python 
files since utf-8 encoded Python files are required to have either the BOM mark or an 
encoding comment.</span></font></div><div align="left">&nbsp;&nbsp;</div>

</body></html>
